package com.voicechat.firstmet.activity;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.voicechat.firstmet.R;
import com.voicechat.firstmet.adapter.MyFriendingListAdapter;
import com.voicechat.firstmet.base.BaseActivity;
import com.voicechat.firstmet.bean.FansAndLike;
import com.voicechat.firstmet.bean.MySelfInfoBean;
import com.voicechat.firstmet.net.HTTPAPI;
import com.voicechat.firstmet.net.service.MeInfoService;
import com.voicechat.firstmet.utils.ParcelableUtils;
import com.voicechat.firstmet.utils.PhoneUtils;
import com.voicechat.firstmet.utils.SPUtils;
import com.voicechat.firstmet.utils.StatusBarUtils;
import com.voicechat.firstmet.utils.Utils;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @ProjectName: FirstMet
 * @Package: com.voicechat.firstmet.activity_imessage
 * @ClassName: MyFriendingListActivity
 * @Description: @好友列表
 * @Author: ljp
 * @CreateDate: 2018/10/18 14:27
 * @Version: 1.0
 */
public class MyFriendingListActivity extends BaseActivity {
    @BindView(R.id.linear_back)
    LinearLayout linearBack;
    @BindView(R.id.recycler_list)
    RecyclerView recyclerList;
    @BindView(R.id.smart_refresh)
    SmartRefreshLayout smartRefresh;


    private MyFriendingListAdapter myFriendingListAdapter;
    private MySelfInfoBean.ObjectBean mySeleInfo;
    private int page;
    private ArrayList<FansAndLike.PageBean.ListBean> listBeans;
    private String TAG = "MyFriendingListActivity";

    @Override
    protected int setStatusBarColor() {
        return 0;
    }

    @Override
    protected boolean isSettingStatusBarText() {
        return false;
    }

    @Override
    protected int loadLayout() {
        return R.layout.activity_my_friendinglist;
    }

    @Override
    protected void initLayout(Bundle savedInstanceState) {
        ButterKnife.bind(this);
        //透明状态栏
        StatusBarUtils.Full(this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            this.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }
        mySeleInfo = MySelfInfoBean.ObjectBean.CREATOR.createFromParcel(ParcelableUtils.unmarshall(
                SPUtils.getEditoString(SPUtils.SP_MYSELFINFO)));
        initView();

    }

    private void initView() {
        page = 1;
        listBeans = new ArrayList<>();
        //设置 Header 为 贝塞尔雷达 样式
        smartRefresh.setRefreshHeader(new ClassicsHeader(context));
        smartRefresh.setRefreshFooter(new ClassicsFooter(context));
        //下拉刷新
        smartRefresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                initHttp();
                smartRefresh.finishRefresh(10000);
            }
        });
        //上拉加载更多
        smartRefresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                initHttp();
                smartRefresh.finishLoadMore(10000);
            }
        });
        myFriendingListAdapter = new MyFriendingListAdapter(context, listBeans, R.layout.item_friending_list);
        recyclerList.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
        recyclerList.setAdapter(myFriendingListAdapter);
        myFriendingListAdapter.setItemClick(new MyFriendingListAdapter.ItemClick() {
            @Override
            public void setItemClick(View v, FansAndLike.PageBean.ListBean listBean) {
                Intent intent = getIntent();
                intent.putExtra("name", "@"+listBean.getNickName());
                setResult(RESULT_OK, intent);
                finish();
            }
        });
        initHttp();
    }

    /**
     * @return
     * @method
     * @description 请求我关注信息
     * @date: 2018/11/19 14:29
     * @author: ljp
     */
    private void initHttp() {
        MeInfoService.myLikeList(SPUtils.getEditoString(SPUtils.SP_TOKEN), PhoneUtils.getDeviceInfo(context), mySeleInfo.getId(), page + "").subscribe(fansAndLike -> {
            Log.i(TAG, "httpList: ---------" + fansAndLike.getPage().getList().toString());
            if (fansAndLike.getCode() == HTTPAPI.RETURN_CODE_TYPE.RETRUN_SUCCESS) {
                int i = fansAndLike.getPage().getCount() / fansAndLike.getPage().getPageSize() +
                        (fansAndLike.getPage().getCount() % fansAndLike.getPage().getPageSize() == 0 ? 0 : 1);
                //int i = postBean.getPage().getCount() % postBean.getPage().getPageSize();
                if (page > i) {//判断是否是最后一页
                    smartRefresh.finishLoadMoreWithNoMoreData();//完成加载并标记没有更多数据 1.0.4
                    // Log.i(TAG, "communityPostList: -------------postLists.size();--------" + postLists.size());
                } else {
                    if (page == 1) {
                        listBeans.clear();
                        smartRefresh.finishRefresh();
                    }
                    listBeans.addAll(fansAndLike.getPage().getList());
                    myFriendingListAdapter.notifyDataSetChanged();
                    smartRefresh.finishLoadMore();
                }
            }else if(fansAndLike.getCode() == HTTPAPI.RETURN_CODE_TYPE.RETURN_NO_TOKEN) {
                Utils.setToken(context);
            }else {
                toast_MakeText(fansAndLike.getMsg());
            }
        }, throwable -> {
            Log.i(TAG, "httpList: ----" + throwable.toString());
        });
    }


    @OnClick(R.id.linear_back)
    public void onClick() {
        finish();
    }
}
